package jok.charset;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import jok.io.FileDemo;

public class CharsetDemo {

	public static void main(String[] args) {

		writeFile();

		readFile();

		FileDemo.deleteFile("D:/a.txt");
	}

	public static void writeFile() {
		OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(new FileOutputStream("D:/a.txt"),
					"GBK");
			osw.write("你好，再见！");
			osw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (osw != null)
					osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void readFile() {
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream("D:/a.txt"), "GBK");
			char[] cs = new char[1024];
			int len = isr.read(cs);
			System.out.println(new String(cs, 0, len));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (isr != null)
					isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
